<!DOCTYPE html>
<html>
<head><meta charset="utf-8" />
<title>101-基本语法</title>

<script src="/lib/require/index.js"></script>
<script src="/lib/jquery/index.js"></script>

<!-- 改为引入外部样式 -->
<link rel="stylesheet" href="/css/custom.css">

<!-- Loading mathjax macro -->
<!-- Load mathjax -->
    <script src="/lib/mathjax/index.js"></script>
    <!-- MathJax configuration -->
    <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ['$','$'], ["\\(","\\)"] ],
            displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
            processEscapes: true,
            processEnvironments: true
        },
        // Center justify equations in code and markdown cells. Elsewhere
        // we use CSS to left justify single line equations in code cells.
        displayAlign: 'center',
        "HTML-CSS": {
            styles: {'.MathJax_Display': {"margin": 0}},
            linebreaks: { automatic: true }
        }
    });
    </script>
    <!-- End of mathjax configuration --></head>
<body>
<div class="toc">
<ul>
                        
                        
                        <li class="toc-li-2">
                            <a href="#python3基础语法">python3基础语法</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#Linux环境下编程">Linux环境下编程</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#编译">编译</a>
                        </li>
                    
                
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#编码">编码</a>
                        </li>
                    
                
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#标识符(给对象起名字)">标识符(给对象起名字)</a>
                        </li>
                    
                
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#python保留字">python保留字</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#注释">注释</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#缩进与空格">缩进与空格</a>
                        </li>
                    
                
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#代码换行">代码换行</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-2">
                            <a href="#数据类型">数据类型</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#Number（数字）">Number（数字）</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#String">String</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#List">List</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#Tuple">Tuple</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#Dictionary-----{key:value}">Dictionary --- {key:value}</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#Set-----{1,'a'}">Set --- {1,'a'}</a>
                        </li>
                    
                
            
            
            
            
            
                        
                        
                        <li class="toc-li-2">
                            <a href="#变量">变量</a>
                        </li>
                    
                
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-2">
                            <a href="#Python3运算符">Python3运算符</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#算术运算符">算术运算符</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#比较运算符">比较运算符</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#赋值运算符">赋值运算符</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#位运算符">位运算符</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#逻辑运算符">逻辑运算符</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#成员运算符">成员运算符</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#身份运算符">身份运算符</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#运算符优先级">运算符优先级</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#float精度问题">float精度问题</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-2">
                            <a href="#常用数学函数">常用数学函数</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#math模块">math模块</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#随机数函数">随机数函数</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            </ul>
</div>
  <div tabindex="-1" id="notebook" class="border-box-sizing">
    <div class="container" id="notebook-container">

<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 打印所有单行变量</span>
<span class="kn">from</span> <span class="nn">IPython.core.interactiveshell</span> <span class="k">import</span> <span class="n">InteractiveShell</span>
<span class="n">InteractiveShell</span><span class="o">.</span><span class="n">ast_node_interactivity</span> <span class="o">=</span> <span class="s1">&#39;all&#39;</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="python3&#22522;&#30784;&#35821;&#27861;">python3&#22522;&#30784;&#35821;&#27861;<a class="anchor-link" href="#python3&#22522;&#30784;&#35821;&#27861;">&#182;</a></h2><p>python是一种解释型语言</p>
<h3 id="Linux&#29615;&#22659;&#19979;&#32534;&#31243;">Linux&#29615;&#22659;&#19979;&#32534;&#31243;<a class="anchor-link" href="#Linux&#29615;&#22659;&#19979;&#32534;&#31243;">&#182;</a></h3><p>打开终端输入python3即可进入python3 shell，要打开py文件直接在py文件所在位置终端输入python3 ***.py，或输入绝对路径打开</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="ch">#!/usr/bin/env python3  # linux系统下必须，这是python3的环境变量</span>
<span class="c1">#coding:utf-8  # 申明编码，非必须，便于阅读和维护</span>

<span class="n">a</span> <span class="o">=</span> <span class="mi">19</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">-</span> <span class="mi">8</span> <span class="o">/</span> <span class="mi">2</span>  <span class="c1"># 表达式</span>
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>  <span class="c1"># 函数</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>23.0
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#32534;&#35793;">&#32534;&#35793;<a class="anchor-link" href="#&#32534;&#35793;">&#182;</a></h3><p>当运行<code>.py</code>文件的时候，Python会通过编译器，将它编译为<code>.pyc</code>文件。
然后这个文件就在一个名为虚拟机的东西上运行，这个所谓的虚拟机是专门为Python设计的。有了虚拟机，使得Python可以跨平台</p>
<h3 id="&#32534;&#30721;">&#32534;&#30721;<a class="anchor-link" href="#&#32534;&#30721;">&#182;</a></h3><p>默认情况下，Python 3 源码文件以 UTF-8 编码，所有字符串都是 unicode 字符串。也可以为源码文件指定不同的编码：</p>

<pre><code># -*- coding: cp-1252 -*-

</code></pre>
<h3 id="&#26631;&#35782;&#31526;(&#32473;&#23545;&#35937;&#36215;&#21517;&#23383;)">&#26631;&#35782;&#31526;(&#32473;&#23545;&#35937;&#36215;&#21517;&#23383;)<a class="anchor-link" href="#&#26631;&#35782;&#31526;(&#32473;&#23545;&#35937;&#36215;&#21517;&#23383;)">&#182;</a></h3><blockquote><p>一切皆可为对象</p>
<ul>
<li>第一个字符必须是字母表中字母或下划线<code>_</code>。</li>
<li>标识符的其他的部分有字母、数字和下划线组成。</li>
<li>标识符对大小写敏感。</li>
</ul>
</blockquote>
<h3 id="python&#20445;&#30041;&#23383;">python&#20445;&#30041;&#23383;<a class="anchor-link" href="#python&#20445;&#30041;&#23383;">&#182;</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">keyword</span>

<span class="nb">print</span><span class="p">(</span><span class="n">keyword</span><span class="o">.</span><span class="n">kwlist</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>[&#39;False&#39;, &#39;None&#39;, &#39;True&#39;, &#39;and&#39;, &#39;as&#39;, &#39;assert&#39;, &#39;break&#39;, &#39;class&#39;, &#39;continue&#39;, &#39;def&#39;, &#39;del&#39;, &#39;elif&#39;, &#39;else&#39;, &#39;except&#39;, &#39;finally&#39;, &#39;for&#39;, &#39;from&#39;, &#39;global&#39;, &#39;if&#39;, &#39;import&#39;, &#39;in&#39;, &#39;is&#39;, &#39;lambda&#39;, &#39;nonlocal&#39;, &#39;not&#39;, &#39;or&#39;, &#39;pass&#39;, &#39;raise&#39;, &#39;return&#39;, &#39;try&#39;, &#39;while&#39;, &#39;with&#39;, &#39;yield&#39;]
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#27880;&#37322;">&#27880;&#37322;<a class="anchor-link" href="#&#27880;&#37322;">&#182;</a></h3><blockquote><p>注释的作用便于读写和维护代码</p>
</blockquote>
<p>单行注释用#</p>

<pre><code># 程序运行时该注释不会显示

</code></pre>
<p>多行注释用三对引号</p>

<pre><code>"""
多行注释
多行注释
"""

</code></pre>
<h3 id="&#32553;&#36827;&#19982;&#31354;&#26684;">&#32553;&#36827;&#19982;&#31354;&#26684;<a class="anchor-link" href="#&#32553;&#36827;&#19982;&#31354;&#26684;">&#182;</a></h3><ul>
<li>python最具特色的就是使用缩进来表示代码块，不需要使用大括号<code>{}</code>。
缩进的空格数是可变的，但是同一个代码块的语句必须包含相同的缩进空格数，为了统一规范一般用4个空格。</li>
<li>为了代码的整体美观、便于阅读和便于维护，用空格隔开不同的元素</li>
</ul>
<h3 id="&#20195;&#30721;&#25442;&#34892;">&#20195;&#30721;&#25442;&#34892;<a class="anchor-link" href="#&#20195;&#30721;&#25442;&#34892;">&#182;</a></h3><ul>
<li>Python 通常是一行写完一条语句，但如果语句很长，我们可以使用反斜杠<code>\</code>来实现多行语句，例如：</li>
</ul>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">item_one</span> <span class="o">=</span> <span class="n">item_two</span> <span class="o">=</span> <span class="n">item_three</span> <span class="o">=</span><span class="mi">1</span>
<span class="n">total</span> <span class="o">=</span> <span class="n">item_one</span> <span class="o">+</span> \
        <span class="n">item_two</span> <span class="o">+</span> \
        <span class="n">item_three</span>

<span class="n">total</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[7]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>3</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ul>
<li>在<code>[]</code> <code>{}</code>或<code>()</code>中的多行语句，不需要使用反斜杠<code>\</code>，例如：</li>
</ul>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">total</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;item_one&#39;</span><span class="p">,</span> <span class="s1">&#39;item_two&#39;</span><span class="p">,</span> <span class="s1">&#39;item_three&#39;</span><span class="p">,</span>
        <span class="s1">&#39;item_four&#39;</span><span class="p">,</span> <span class="s1">&#39;item_five&#39;</span><span class="p">]</span>
<span class="n">total</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[8]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[&#39;item_one&#39;, &#39;item_two&#39;, &#39;item_three&#39;, &#39;item_four&#39;, &#39;item_five&#39;]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ul>
<li><p>一行显示多条语句,Python可以在同一行中使用多条语句，语句之间使用分号<code>;</code>分割:</p>

<pre><code>import sys; x = 'runoob'; sys.stdout.write(x + '\n')</code></pre>
</li>
<li><p>空行<br>
  函数之间或类的方法之间用空行分隔，表示一段新的代码的开始。类和函数入口之间也用一行空行分隔，以突出函数入口的开始。
  空行与代码缩进不同，空行并不是Python语法的一部分。书写时不插入空行，Python解释器运行也不会出错。但是空行的作用在于分隔两段不同功能或含义的代码，便于日后代码的维护或重构。</p>
</li>
</ul>
<blockquote><p>在Python中的变量不需要声明。每个变量在使用前都必须赋值，变量赋值以后该变量才会被创建。<strong>对象有类型，变量无类型</strong></p>
</blockquote>
<h2 id="&#25968;&#25454;&#31867;&#22411;">&#25968;&#25454;&#31867;&#22411;<a class="anchor-link" href="#&#25968;&#25454;&#31867;&#22411;">&#182;</a></h2><h3 id="Number&#65288;&#25968;&#23383;&#65289;">Number&#65288;&#25968;&#23383;&#65289;<a class="anchor-link" href="#Number&#65288;&#25968;&#23383;&#65289;">&#182;</a></h3><ul>
<li>int</li>
<li>float</li>
<li>bool<br>
  <code>True</code> <code>False</code>，首字母必须大写</li>
<li>complex
  1+2j</li>
</ul>
<h3 id="String">String<a class="anchor-link" href="#String">&#182;</a></h3><p>可以索引和切片，不能被改变</p>
<h3 id="List">List<a class="anchor-link" href="#List">&#182;</a></h3><p>有序，可以索引、切片、可变</p>
<h3 id="Tuple">Tuple<a class="anchor-link" href="#Tuple">&#182;</a></h3><p>可以索引和切片，不能被改变</p>
<h3 id="Dictionary-----{key:value}">Dictionary --- {key:value}<a class="anchor-link" href="#Dictionary-----{key:value}">&#182;</a></h3><p>无序，元素是一对键值</p>
<h3 id="Set-----{1,'a'}">Set --- {1,'a'}<a class="anchor-link" href="#Set-----{1,'a'}">&#182;</a></h3><p>集合是一个无序不重复元素的序列。创建一个空集合必须用<code>set()</code>而不是<code>{}</code>，因为<code>{}</code>是用来创建一个空字典。</p>
<blockquote><p>id()可以查看内存地址
type()查看数据类型</p>
</blockquote>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="&#21464;&#37327;">&#21464;&#37327;<a class="anchor-link" href="#&#21464;&#37327;">&#182;</a></h2><p>在Python中，有这样一句话是非常重要的：<strong>对象有类型，变量无类型</strong>。
Python 中的变量不需要声明。每个变量在使用前都必须赋值，变量赋值以后该变量才会被创建。
在 Python 中，变量就是变量，它没有类型，我们所说的"类型"是变量所指的内存中对象的类型。
等号<code>=</code>用来给变量赋值。
等号<code>=</code>运算符左边是一个变量名,等号<code>=</code>运算符右边是存储在变量中的值</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Python3&#36816;&#31639;&#31526;">Python3&#36816;&#31639;&#31526;<a class="anchor-link" href="#Python3&#36816;&#31639;&#31526;">&#182;</a></h2><ul>
<li>算术运算符</li>
<li>比较（关系）运算符</li>
<li>赋值运算符</li>
<li>逻辑运算符</li>
<li>位运算符</li>
<li>成员运算符</li>
<li>身份运算符</li>
<li>运算符优先级</li>
</ul>
<h3 id="&#31639;&#26415;&#36816;&#31639;&#31526;">&#31639;&#26415;&#36816;&#31639;&#31526;<a class="anchor-link" href="#&#31639;&#26415;&#36816;&#31639;&#31526;">&#182;</a></h3>
<pre><code>&gt;&gt;&gt; a = 10
&gt;&gt;&gt; b = 4

</code></pre>
<table>
<thead><tr>
<th style="text-align:center">运算符</th>
<th style="text-align:center">描述</th>
<th style="text-align:center">实例</th>
<th style="text-align:center">结果</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">+</td>
<td style="text-align:center">加</td>
<td style="text-align:center">a+b</td>
<td style="text-align:center">14</td>
</tr>
<tr>
<td style="text-align:center">-</td>
<td style="text-align:center">减</td>
<td style="text-align:center">a-b</td>
<td style="text-align:center">6</td>
</tr>
<tr>
<td style="text-align:center">*</td>
<td style="text-align:center">乘</td>
<td style="text-align:center">a*b</td>
<td style="text-align:center">40</td>
</tr>
<tr>
<td style="text-align:center">/</td>
<td style="text-align:center">除</td>
<td style="text-align:center">a/b</td>
<td style="text-align:center"><span style="color:blue;" title="结果的类型总是为float">2.5</span></td>
</tr>
<tr>
<td style="text-align:center">%</td>
<td style="text-align:center">取余</td>
<td style="text-align:center">a%b</td>
<td style="text-align:center">2</td>
</tr>
<tr>
<td style="text-align:center">**</td>
<td style="text-align:center">幂</td>
<td style="text-align:center">a**b</td>
<td style="text-align:center"><span style="color:blue;" title="若数字太长则会用科学计数法显示">10000</span></td>
</tr>
<tr>
<td style="text-align:center">//</td>
<td style="text-align:center">取整除</td>
<td style="text-align:center">a//b</td>
<td style="text-align:center">2</td>
</tr>
</tbody>
</table>
<blockquote><p>凡是浮点型参与计算，结果都为浮点型</p>
</blockquote>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#27604;&#36739;&#36816;&#31639;&#31526;">&#27604;&#36739;&#36816;&#31639;&#31526;<a class="anchor-link" href="#&#27604;&#36739;&#36816;&#31639;&#31526;">&#182;</a></h3>
<pre><code>&gt;&gt;&gt; a = 10
&gt;&gt;&gt; b = 4

</code></pre>
<table>
<thead><tr>
<th style="text-align:center">运算符</th>
<th style="text-align:center">描述</th>
<th style="text-align:center">实例</th>
<th style="text-align:center">结果</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">==</td>
<td style="text-align:center">等于</td>
<td style="text-align:center">a==b</td>
<td style="text-align:center">False</td>
</tr>
<tr>
<td style="text-align:center">!=</td>
<td style="text-align:center">不等于</td>
<td style="text-align:center">a!=b</td>
<td style="text-align:center">True</td>
</tr>
<tr>
<td style="text-align:center">&gt;</td>
<td style="text-align:center">大于</td>
<td style="text-align:center">a&gt;b</td>
<td style="text-align:center">True</td>
</tr>
<tr>
<td style="text-align:center">&lt;</td>
<td style="text-align:center">小于</td>
<td style="text-align:center">a&lt;b</td>
<td style="text-align:center">False</td>
</tr>
<tr>
<td style="text-align:center">&gt;=</td>
<td style="text-align:center">大于等于</td>
<td style="text-align:center">a&gt;=b</td>
<td style="text-align:center">False</td>
</tr>
<tr>
<td style="text-align:center">&lt;=</td>
<td style="text-align:center">小于等于</td>
<td style="text-align:center">a&lt;=b</td>
<td style="text-align:center">False</td>
</tr>
</tbody>
</table>
<h3 id="&#36171;&#20540;&#36816;&#31639;&#31526;">&#36171;&#20540;&#36816;&#31639;&#31526;<a class="anchor-link" href="#&#36171;&#20540;&#36816;&#31639;&#31526;">&#182;</a></h3><table>
<thead><tr>
<th style="text-align:center">运算符</th>
<th style="text-align:center">实例</th>
<th style="text-align:center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">=</td>
<td style="text-align:center">c=a+b</td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center">+=</td>
<td style="text-align:center">c+=a</td>
<td style="text-align:center">c=c+a</td>
</tr>
<tr>
<td style="text-align:center">-=</td>
<td style="text-align:center">c-=a</td>
<td style="text-align:center">c=c-a</td>
</tr>
<tr>
<td style="text-align:center">*=</td>
<td style="text-align:center">c*=a</td>
<td style="text-align:center">c=c*a</td>
</tr>
<tr>
<td style="text-align:center">/=</td>
<td style="text-align:center">c/=a</td>
<td style="text-align:center">c=c/a</td>
</tr>
<tr>
<td style="text-align:center">%=</td>
<td style="text-align:center">c%=a</td>
<td style="text-align:center">c=c%a</td>
</tr>
<tr>
<td style="text-align:center">**=</td>
<td style="text-align:center">c**=a</td>
<td style="text-align:center">c=c**a</td>
</tr>
<tr>
<td style="text-align:center">//=</td>
<td style="text-align:center">c//=a</td>
<td style="text-align:center">c=c//a</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#20301;&#36816;&#31639;&#31526;">&#20301;&#36816;&#31639;&#31526;<a class="anchor-link" href="#&#20301;&#36816;&#31639;&#31526;">&#182;</a></h3><blockquote><p>按位运算符是把数字看作二进制来进行计算的。</p>
</blockquote>
<table>
<thead><tr>
<th style="text-align:center">运算符</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">&amp;</td>
<td>按位与运算符；参与运算符的两个值，如果两个相应位都为1，则该位的结果为1，否则为0</td>
</tr>
<tr>
<td style="text-align:center">按位或</td>
<td>按位或运算符：只要对应的二个二进位有一个为1时，结果位就为1。</td>
</tr>
<tr>
<td style="text-align:center">^</td>
<td>按位异或运算符；当两队应的二进位相异时，结果为1</td>
</tr>
<tr>
<td style="text-align:center">~</td>
<td>按位取反运算符：对数据的每个二进制位取反,即把1变为0,把0变为1</td>
</tr>
<tr>
<td style="text-align:center">&lt;&lt;</td>
<td>左移动运算符：运算数的各二进位全部左移若干位，由"&lt;&lt;"右边的数指定移动的位数，高位丢弃，低位补0。</td>
</tr>
<tr>
<td style="text-align:center">&gt;&gt;</td>
<td>右移动运算符：把"&gt;&gt;"左边的运算数的各二进位全部右移若干位，"&gt;&gt;"右边的数指定移动的位数</td>
</tr>
</tbody>
</table>
<blockquote><p>按位或运算符<code>|</code>，md表格不能打出<code>|</code></p>
</blockquote>
<h3 id="&#36923;&#36753;&#36816;&#31639;&#31526;">&#36923;&#36753;&#36816;&#31639;&#31526;<a class="anchor-link" href="#&#36923;&#36753;&#36816;&#31639;&#31526;">&#182;</a></h3><p><code>and</code>, <code>or</code>, <code>not</code></p>
<h3 id="&#25104;&#21592;&#36816;&#31639;&#31526;">&#25104;&#21592;&#36816;&#31639;&#31526;<a class="anchor-link" href="#&#25104;&#21592;&#36816;&#31639;&#31526;">&#182;</a></h3><p><code>in</code>, <code>not in</code></p>
<h3 id="&#36523;&#20221;&#36816;&#31639;&#31526;">&#36523;&#20221;&#36816;&#31639;&#31526;<a class="anchor-link" href="#&#36523;&#20221;&#36816;&#31639;&#31526;">&#182;</a></h3><p><code>is</code>, <code>is not</code></p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#36816;&#31639;&#31526;&#20248;&#20808;&#32423;">&#36816;&#31639;&#31526;&#20248;&#20808;&#32423;<a class="anchor-link" href="#&#36816;&#31639;&#31526;&#20248;&#20808;&#32423;">&#182;</a></h3><table>
<thead><tr>
<th style="text-align:center">优先级</th>
<th style="text-align:center">运算符</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">1</td>
<td style="text-align:center"><code>**</code></td>
</tr>
<tr>
<td style="text-align:center">2</td>
<td style="text-align:center"><code>~</code> <span style="color:blue;" title="一元加号和减号">+ -</span></td>
</tr>
<tr>
<td style="text-align:center">3</td>
<td style="text-align:center"><code>*</code> <code>/</code> <code>%</code> <code>//</code></td>
</tr>
<tr>
<td style="text-align:center">4</td>
<td style="text-align:center"><code>+</code> <code>-</code></td>
</tr>
<tr>
<td style="text-align:center">5</td>
<td style="text-align:center"><code>&gt;&gt;</code> <code>&lt;&lt;</code></td>
</tr>
<tr>
<td style="text-align:center">6</td>
<td style="text-align:center"><code>&amp;</code></td>
</tr>
<tr>
<td style="text-align:center">7</td>
<td style="text-align:center"><code>^</code> 按位或</td>
</tr>
<tr>
<td style="text-align:center">8</td>
<td style="text-align:center"><code>&lt;=</code> <code>&lt;&gt;</code> <code>&gt;=</code></td>
</tr>
<tr>
<td style="text-align:center">9</td>
<td style="text-align:center"><code>&lt;``&gt;</code> <code>==</code> <code>!=</code></td>
</tr>
<tr>
<td style="text-align:center">10</td>
<td style="text-align:center"><code>=</code> <code>%=</code> <code>/=</code> <code>//=</code> <code>-=</code> <code>+=</code> <code>*=</code> <code>**=</code></td>
</tr>
<tr>
<td style="text-align:center">11</td>
<td style="text-align:center"><code>is</code> <code>is not</code></td>
</tr>
<tr>
<td style="text-align:center">12</td>
<td style="text-align:center"><code>in</code> <code>not in</code></td>
</tr>
<tr>
<td style="text-align:center">13</td>
<td style="text-align:center"><code>not</code> <code>or</code> <code>and</code></td>
</tr>
</tbody>
</table>
<h3 id="float&#31934;&#24230;&#38382;&#39064;">float&#31934;&#24230;&#38382;&#39064;<a class="anchor-link" href="#float&#31934;&#24230;&#38382;&#39064;">&#182;</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.2</span>              <span class="c1"># --&gt;</span>

<span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.1</span> <span class="o">-</span> <span class="mf">0.3</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[3]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>0.30000000000000004</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[3]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>5.551115123125783e-17</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>原因在于十进制和二进制的转换上，电脑用的是二进制进行计算，上面的例子中，我们输入的是十进制，她就要把十进制的数转化为二进制，然后再计算。但是，在转化中，浮点数转化为二进制，就出问题了。</p>
<p>例如十进制的0.1，转化为二进制是：0.0001100110011001100110011001100110011001100110011...</p>
<p>也就是说，转化为二进制后，不会精确等于十进制的0.1。同时，计算机存储的位数是有限制的，所以，就出现上述现象了。
这种问题不仅仅是Python中有，所有支持浮点数运算的编程语言都会遇到，它不是Python的bug。</p>
<blockquote><p>使用numPy包和其它用于数学和统计学的包，可以解决上述问题</p>
</blockquote>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="&#24120;&#29992;&#25968;&#23398;&#20989;&#25968;">&#24120;&#29992;&#25968;&#23398;&#20989;&#25968;<a class="anchor-link" href="#&#24120;&#29992;&#25968;&#23398;&#20989;&#25968;">&#182;</a></h2><table>
<thead><tr>
<th style="text-align:center">函数</th>
<th style="text-align:center">代码实例</th>
<th style="text-align:center">返回值</th>
<th style="text-align:center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">abs(x)</td>
<td style="text-align:center">abs(-1.2)</td>
<td style="text-align:center">1.2</td>
<td style="text-align:center">返回 x（数字）的绝对值，如果参数是一个复数，则返回它的大小</td>
</tr>
<tr>
<td style="text-align:center">max(x)</td>
<td style="text-align:center">max(1,2)</td>
<td style="text-align:center">2</td>
<td style="text-align:center">返回给定参数的最大值，参数可以为序列</td>
</tr>
<tr>
<td style="text-align:center">min(x)</td>
<td style="text-align:center">min(1,2)</td>
<td style="text-align:center">1</td>
<td style="text-align:center">返回给定参数的最小值，参数可以为序列</td>
</tr>
<tr>
<td style="text-align:center">round(x[,n])</td>
<td style="text-align:center">round(3.14,1)</td>
<td style="text-align:center">3.1</td>
<td style="text-align:center">返回浮点数x的四舍五入值,第二个参数规定保留的小数位数，不写则默认为0</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="math&#27169;&#22359;">math&#27169;&#22359;<a class="anchor-link" href="#math&#27169;&#22359;">&#182;</a></h3><p>math是标准库之一，所以不用安装，可以直接使用。使用方法是：</p>

<pre><code>&gt;&gt;&gt; import math

</code></pre>
<table>
<thead><tr>
<th style="text-align:center">函数</th>
<th style="text-align:center">代码实例</th>
<th style="text-align:center">返回值</th>
<th style="text-align:center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">ceil(x)</td>
<td style="text-align:center">math.ceil(1.2)</td>
<td style="text-align:center">2</td>
<td style="text-align:center">返回一个大于或等于 x 的的最小整数</td>
</tr>
<tr>
<td style="text-align:center">floor(x)</td>
<td style="text-align:center">math.floor(-1.2)</td>
<td style="text-align:center">-2</td>
<td style="text-align:center">返回一个小于或等于 x 的的最小整数</td>
</tr>
<tr>
<td style="text-align:center">exp(x)</td>
<td style="text-align:center">math.exp(math.pi)</td>
<td style="text-align:center">23.140692632779267</td>
<td style="text-align:center">返回x的指数,e<sup>x</sup></td>
</tr>
<tr>
<td style="text-align:center">fabs(x)</td>
<td style="text-align:center">math.fabs(-1.2)</td>
<td style="text-align:center">1.2</td>
<td style="text-align:center">返回数字的绝对值,只对浮点型跟整型数值有效</td>
</tr>
<tr>
<td style="text-align:center">sqrt(x)</td>
<td style="text-align:center">math.sqrt(100)</td>
<td style="text-align:center">10</td>
<td style="text-align:center">返回数字x的平方根</td>
</tr>
</tbody>
</table>
<p><code>dir(module)</code>：查看命令的属性和方法<br>
<code>help()</code>：查看帮助</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#38543;&#26426;&#25968;&#20989;&#25968;">&#38543;&#26426;&#25968;&#20989;&#25968;<a class="anchor-link" href="#&#38543;&#26426;&#25968;&#20989;&#25968;">&#182;</a></h3><p>随机数可以用于数学，游戏，安全等领域中，还经常被嵌入到算法中，用以提高算法效率，并提高程序的安全性。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">random</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ul>
<li>choice(seq)方法返回一个列表，元组或字符串的随机项</li>
</ul>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="s1">&#39;Runoob&#39;</span><span class="p">)</span>         <span class="c1"># --&gt;</span>
<span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">9</span><span class="p">])</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[6]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>&#39;u&#39;</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[6]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>1</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ul>
<li>randrange ([start,] stop [,step])方法返回指定递增基数集合中的一个随机数，基数缺省值为1。<ul>
<li>start -- 指定范围内的开始值，包含在范围内。</li>
<li>stop -- 指定范围内的结束值，不包含在范围内。</li>
<li>step -- 指定递增基数。</li>
</ul>
</li>
</ul>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 从 1-100 中选取一个奇数</span>
<span class="n">random</span><span class="o">.</span><span class="n">randrange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[14]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>21</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 从 0-99 选取一个随机数</span>
<span class="n">random</span><span class="o">.</span><span class="n">randrange</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[9]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>65</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># random() 方法返回随机生成的一个实数，它在[0,1)范围内。</span>
<span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[11]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>0.8046523334845248</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ul>
<li>random.seed([x])改变随机数生成器的种子,此方法没有返回值。参数x 改变随机数生成器的种子seed。不必特别去设定seed，Python会选择seed。</li>
<li>random.shuffle(lst)方法将列表和元组的所有元素随机排序。</li>
</ul>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">list1</span> <span class="o">=</span> <span class="p">[</span><span class="mi">20</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">5</span><span class="p">]</span>
<span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="n">list1</span><span class="p">)</span>
<span class="n">list1</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[15]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[20, 5, 16, 10]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># uniform(x, y)在[x,y]范围内,随机生成一个实数。</span>
<span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[16]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>6.1802185380961685</pre>
</div>

</div>

</div>
</div>

</div>
    </div>
  </div>
</body>

 


</html>
